package org.jeecg.modules.daily.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.daily.entity.Daily;
import org.jeecg.modules.daily.entity.DailyZi;
import org.jeecg.modules.daily.mapper.DailyMapper;
import org.jeecg.modules.daily.mapper.DailyZiMapper;
import org.jeecg.modules.daily.service.IDailyService;
import org.jeecg.modules.ganttTasks.entity.GanttTasks;
import org.jeecg.modules.ganttTasks.service.IGanttTasksService;
import org.jeecg.modules.personalTask.entity.PersonalTask;
import org.jeecg.modules.personalTask.service.IPersonalTaskService;
import org.jeecg.modules.system.util.ThreadPoolManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * @Description: 任务汇报
 * @Author: jeecg-boot
 * @Date:   2024-02-23
 * @Version: V1.0
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements IDailyService {

    @Autowired
    private DailyMapper dailyMapper;
    @Autowired
    private DailyZiMapper dailyZiMapper;
    @Autowired
    private IPersonalTaskService personalTaskService;
    @Autowired
    private IGanttTasksService ganttTasksService;
    @Autowired
    private ThreadPoolManager threadPoolManager;

    @Override
    @Transactional
    public void saveMain(Daily daily, List<DailyZi> dailyZiList) {
        dailyMapper.insert(daily);
        saveDailyZi(daily, dailyZiList);
    }

    @Override
    public void updateMain(Daily daily, List<DailyZi> dailyZiList) {
        dailyMapper.updateById(daily);
        //1.先删除子表数据
        LambdaQueryWrapper<DailyZi> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DailyZi::getDailyId, daily.getId());
        dailyZiMapper.delete(wrapper);
        //2.子表数据重新插入
        saveDailyZi(daily, dailyZiList);
    }

    @Override
    public void delMain(String id) {
        LambdaQueryWrapper<DailyZi> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DailyZi::getDailyId, id);
        dailyZiMapper.delete(wrapper);
        dailyMapper.deleteById(id);
    }

    @Override
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            LambdaQueryWrapper<DailyZi> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(DailyZi::getDailyId, id.toString());
            dailyZiMapper.delete(wrapper);
            dailyMapper.deleteById(id);
        }
    }

    private void saveDailyZi(Daily daily, List<DailyZi> dailyZiList) {
        if (dailyZiList != null && !dailyZiList.isEmpty()) {
            for (DailyZi entity : dailyZiList) {
                //外键设置
                entity.setDailyId(daily.getId());
                dailyZiMapper.insert(entity);
            }
            threadPoolManager.executeTask(() -> updatePersonalTask(dailyZiList));
        }
    }

    private void updatePersonalTask(List<DailyZi> dailyZiList) {
        Map<String, Double> dailyZiMap = new HashMap<>();
        Map<String, Double> personalTaskMap = new HashMap<>();
        List<String> taskIds = new ArrayList<>();
        List<String> ganttIds = new ArrayList<>();
        List<PersonalTask> updateList = new ArrayList<>();
        List<GanttTasks> ganttTasksList = new ArrayList<>();
        for (DailyZi entity : dailyZiList) {
            taskIds.add(entity.getTaskId());
            dailyZiMap.put(entity.getTaskId(), entity.getProgress());
        }
        List<PersonalTask> list = personalTaskService.listByIds(taskIds);
        for (PersonalTask personalTask : list){
            if (dailyZiMap.containsKey(personalTask.getId())){
                Double v = dailyZiMap.get(personalTask.getId());
                String ganttId = personalTask.getGanttId();
                if (v == 1){
                    personalTask.setStatus(2);
                }
                personalTask.setProgress(v);
                updateList.add(personalTask);
                ganttIds.add(ganttId);
                personalTaskMap.put(ganttId, v);
            }
        }
        List<GanttTasks> ganttTasks = ganttTasksService.listByIds(ganttIds);
        for (GanttTasks gantt : ganttTasks){
            if (personalTaskMap.containsKey(gantt.getId())){
                Double v = personalTaskMap.get(gantt.getId());
                gantt.setProgress(v);
                ganttTasksList.add(gantt);
            }
        }
        ganttTasksService.updateBatchById(ganttTasksList);
        personalTaskService.updateBatchById(updateList);
    }
}
